package org.framework.converter;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;

import java.io.IOException;

public class EnumBaseDeserializer<E extends Enum<?> & EnumBase>
    extends JsonDeserializer<EnumBase> implements ContextualDeserializer {
    private Class<E> targetClass;
    public EnumBaseDeserializer() {
    }
    public EnumBaseDeserializer(Class<E> targetClass) {
        this.targetClass = targetClass;
    }
    @Override
    public E deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return EnumBase.codeOf(targetClass, p.readValueAs(Object.class));
    }
    @Override
    public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
                                    BeanProperty property) throws JsonMappingException {
        targetClass = (Class<E>) ctxt.getContextualType().getRawClass();
        return new EnumBaseDeserializer<>(targetClass);
    }
}